
package uk.ac.cam.cl.oscar.MMS_server;

import java.awt.image.BufferedImage;
import java.util.StringTokenizer;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.Socket;    


public class ImageDownloader {
   
    public String url = "";
    public boolean imagePresent = false;
    public String phoneNo = "";
    public String network = "";
    public void getMMSImageLocation(Socket s){
        
       String httpPost = "";
        
        try{
            /*Setup input and output streams for connection with gateway*/
            InputStream is = s.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = s.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(os);
            PrintWriter pw = new PrintWriter(bos, true);
            
            /*Read message sent by gateway*/
            while(bis.available() > 0){
                httpPost += (char)bis.read();
            }
            
            /*Reply to server to notify that message has been received*/
            //pw.println("HTTP/1.1 200 OK");
            pw.close();
            bis.close();
            
            /*Break down the HTTP post sent by server, and extract the source phone number and image location parameters*/
            StringTokenizer st = new StringTokenizer(httpPost, "&");
            while(st.hasMoreTokens()){
                String temp = st.nextToken();
                if(temp.indexOf("image0") > -1){
                    url=temp;
                    imagePresent = true;
                }
                if(temp.indexOf("source") > -1){
                    phoneNo=temp;
                }
                if(temp.indexOf("network") > -1){
                    network=temp;
                }
                
            }
   
            /*refine strings*/
            url = url.replaceAll("image0=", "");
            url = url.replaceAll("%2F", "/");
            url = url.replaceAll("%3A", ":"); 
            phoneNo = phoneNo.replaceAll("source=", "");
            network = network.replaceAll("network=", "");
        }
        catch(IOException e){
            System.out.printf("IOException occurred in ImageDownloader.parseUrl()");
        }
       
    }
    
    public BufferedImage downloadImage(){
        BufferedImage image = null;
        URL imageURL;
        
        try{
           imageURL = new URL(url);
           image = javax.imageio.ImageIO.read(imageURL);
        }
        catch(MalformedURLException e){
           
        }
        catch(IOException e){
        
        }
        
        return image;
    }
    
        
        
        
        
        
    
    
}
